﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text; 
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using SSPDemo;

namespace SSP
{
    class SSPDirector
    {
        static SSPDirector instance;
        public SSPScene runningScene;
        public SSPGame gameInstance;
        public SSPInput gameInput;
        public SpriteBatch spriteBatchInstance;
        public GraphicsDeviceManager graphicsInstance;
        ArrayList sceneStack;
        public bool isPaused;

        public static SSPDirector Instance
        {
            get { return instance; }
        } 

        public SSPDirector(SSPGame g,SpriteBatch sb,GraphicsDeviceManager gdm)
        {
            gameInstance = g;
            spriteBatchInstance = sb;
            graphicsInstance = gdm;
            instance = this;
            sceneStack = new ArrayList();
        }

        public Vector2 displaySize()
        {
            return new Vector2(gameInstance.GraphicsDevice.Adapter.CurrentDisplayMode.Width, gameInstance.GraphicsDevice.Adapter.CurrentDisplayMode.Height);
        }

        public void draw(SpriteBatch batch, GameTime gameTime)
        {
            //Console.WriteLine("Director Draw()");
            if (runningScene != null)
            {
                foreach (SSPNode n in runningScene.children)
                {
                    n.draw();
                } 
            }
        }

        public void runWithScene(SSPScene s)
        {
            if (s.isInputEnabled)
            {
                gameInput = new SSPInput(s);
            }
            else
            {
            }
           
            runningScene = s;

            //Console.WriteLine("Run with Scene =  {0} ", runningScene.children.Count);

        }

        public Vector2 windowSize()
        {
            return new Vector2(graphicsInstance.PreferredBackBufferWidth, graphicsInstance.PreferredBackBufferHeight);
        
        }

        public void setWindowSize(Vector2 size)
        {
            graphicsInstance.PreferredBackBufferHeight = (int)size.Y;
            graphicsInstance.PreferredBackBufferWidth = (int)size.X;
            graphicsInstance.ApplyChanges();
        }

        public void popScene()
        {
            runningScene = (SSPScene)sceneStack[0];
            sceneStack.Clear();
        }

        public void pushScene(SSPScene s)
        {
            sceneStack.Add(runningScene);

            runningScene = s;
        }

        public void paused()
        {
            isPaused = true;
        }

        public void replaceScene(SSPScene s)
        {
            if (s.isInputEnabled)
            {
                gameInput = new SSPInput(s);
            }
            runningScene = s;
        }

        public void resume()
        {
            isPaused = false;
        }
        public void update(GameTime gameTimes)
        {
            //Console.WriteLine("Director Update()");
            if (runningScene != null && !isPaused)
            {
                if (runningScene.isInputEnabled)
                {

                    gameInput.Update();
                }
                
                runningScene.update();
                /*
                foreach (SSPNode n in runningScene.children)
                {
                    n.update();
                }
                */
            }
        }
    }
}
